package com.example.safeassistan.utile;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtil   {
    private static final String DEAFAULT_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;

        private BitmapUtil() {
        }



/**
 * 保存图片到本地 第一个参数是图片名称 第二个参数为需要保存的bitmap
 * */
public static void saveImgToDisk(String path,String imgName, Bitmap bitmap) {

        File files=new File(DEAFAULT_FILE_PATH+path);
        if(!files.exists()){
               files.mkdirs();
        }
        File file = new File(files,imgName);
        if(!file.exists()) {
                try {
                        file.createNewFile();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        try {
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
        } catch (FileNotFoundException e) {
                 e.printStackTrace();
        } catch (IOException e) {
                         e.printStackTrace();
                 }
        }

        /**
         * 保存视频到本地 第一个参数是图片名称 第二个参数为需要保存的bitmap
         * */
        public static void saveVideoToDisk(String path,String imgName,String oldfile) {

                File files=new File(DEAFAULT_FILE_PATH+path);
                if(!files.exists()){
                        files.mkdir();
                }
                File file = new File(files, imgName);
                if(!file.exists()) {
                        try {
                                file.createNewFile();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }

                try {
                        FileOutputStream out = new FileOutputStream(file);
                        FileInputStream in=new FileInputStream(oldfile);

                        byte[] b = new byte[1024];
                        int tem = 0;
                        while ((tem = in.read(b))!= -1) {
                                out.write(b,0,tem);
                        }
                        in.close();
                        out.flush();
                        out.close();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }


/**
 * 返回SD卡跟目录 <br>
 *
 * @return SD卡跟目录
 */
public static String getSdCardPath() {
        File sdDir ;
        boolean sdCardExist = isSdCardExist(); // 判断sd卡是否存在
        if (sdCardExist) {
        sdDir =Environment.getExternalStorageDirectory();// 获取跟目录
        return sdDir.toString();
        } else {
        return null;
        }
        }

/**
 * 判断SD卡是否存在 <br>
 *
 * @return SD卡存在返回true，否则返回false
 */
public static boolean isSdCardExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        }
        }

